package com.tenement.action;

import com.tenement.domain.TenementHouse;
import com.tenement.domain.TenementImg;
import com.tenement.domain.TenementUser;
import com.tenement.service.HouseService;
import com.tenement.utils.Message;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : HouseAction  //类名
 * @Description : 房屋信息Controller  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-06 11:20  //时间
 */
@Controller
@RequestMapping("houseAction")
public class HouseAction {

    /**
     * 房屋信息服务接口
     */
    @Resource
    HouseService houseService;

    /**
     * 查询所有（条件，分页）
     * @param house
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(TenementHouse house,
                                      @RequestParam(value = "startDate", required = false) String startDate,
                                      @RequestParam(value = "endDate", required = false) String endDate,
                                      @RequestParam("page") Integer page,@RequestParam("limit") Integer limit){

        List<TenementHouse> list=houseService.findAll(house,page,limit,startDate,endDate);
        Long overCount=houseService.overCount(house,startDate,endDate);

        return Tools.maps(overCount,list);
    }

    @RequestMapping("findById")
    @ResponseBody
    public TenementHouse findById(String houseId){
        return houseService.findById(houseId);
    }

    /**
     * 增加或修改
     * @param house
     * @param session
     * @return
     * @throws ParseException
     */
    @RequestMapping("addOrUpdate")
    @ResponseBody
    public Message addOrUpdate(TenementHouse house, HttpSession session, HttpServletRequest request) {
        String ip=request.getRemoteAddr();
        TenementUser user= (TenementUser) session.getAttribute("user");
        int row;
        if(StringUtils.isEmpty(house.getHouseId())){
            row=houseService.add(house,user,ip);
        }else{
            row=houseService.update(house,user,ip);
        }
        return Tools.rows(row);
    }

    /**
     * 房源地址查询
     * @param houseArea
     * @return
     */
    @RequestMapping("findHouseArea")
    @ResponseBody
    public int findHouseArea(String houseArea){
        return houseService.findHouseArea(houseArea);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public Message deleteById(String id,HttpServletRequest request){
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        String ip=request.getRemoteAddr();
        int row=houseService.delete(id,user,ip);
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(@RequestParam(value = "ids[]",required =false)String ids[],HttpServletRequest request){
        String ip=request.getRemoteAddr();
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        int row=houseService.deleteAll(ids,user,ip);
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("selectAll")
    public List<TenementHouse> selectAll(){
        return houseService.selectAll();
    }

    @ResponseBody
    @RequestMapping("selectImg")
    public List<TenementImg> selectImg(String houseId,String imgType){
        return houseService.selectAllImg(houseId,imgType);
    }

    @ResponseBody
    @RequestMapping("/addImg")
    public int addImg(@RequestParam("imgUrl") String imgUrl,@RequestParam("houseId") String houseId){
        return houseService.addImg(imgUrl,houseId);
    }

    @ResponseBody
    @RequestMapping("/deleteImg")
    public int deleteImg(String imgId){
        return houseService.deleteImg(imgId);
    }

    /**
     * 日期格式化
     * @param binder
     */
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }*/
}
